const crypto = require('crypto')

const adminModel = require('../model/adminModel')
const serviceModel = require('../model/serviceModel')
const teamModel = require('../model/teamModel')
const bannerModel = require('../model/bannerModel')

const adminControl = {}

adminControl.putUser = async (req, res) => {
  const user = {
    name: req.body.name,
    pwd: crypto.createHash('md5').update(req.body.pwd).digest('hex')
  }
  await adminModel.addAdministrator(user).then(row => {
    res.json({
      code: 200,
      data: row
    })
  }).catch(err => {
    res.json({
      code: 100,
      data: err
    })
  })
}

adminControl.userLogin = async (req, res) => {
  console.log(req.body)
  const account = req.body.username
  const userpwd = crypto.createHash('md5').update(req.body.userpwd).digest('hex')

  await adminModel.getOneUser(account).then(row => {
    if (row.name && row.userPwd) {
      if (row.userPwd == userpwd) {
        req.session.uname = account
        req.session.login = true

        res.json({
          code: 200,
          message: '登陆成功！',
          detail: {
            username: account
          }
        })
      } else {
        res.json({
          code: 202020,
          message: '用户名或密码错误'
        })
      }
    } else {
      res.json({
        code: 234823,
        message: '用户不存在'
      })
    }
  }).catch(err => {
    res.json({
      code: 100001,
      message: '用户不存在',
      detail: err
    })
  })
}

adminControl.getServiceList = async (req, res) => {
  try {
    const pageno = req.query.pageno || 1
    const pagesize = req.query.pagesize || 10
    const data = await serviceModel.getServiceList(pageno, pagesize)
    const count = await serviceModel.getServiceCount()

    res.json({
      code: 200,
      message: '',
      detail: {
        list: data,
        count: count,
        currentPage: pageno,
        pageSize: pagesize
      }
    })
  } catch(err) {
    res.json({
      code: 10002,
      message: '数据查询错误'
    })
  }
}

adminControl.getTeamList = async (req, res) => {
  const type = req.query.type
  await teamModel.getTeamList(type).then(rows => {
    res.json({
      code: 200,
      message: '',
      detail: rows
    })
  }).catch(err => {
    res.json({
      code: 10001,
      message: '',
      detail: err
    })
  })
}

adminControl.addTeamMember = async (req, res) => {
  const member = {
    name: req.body.name,
    enname: req.body.enname,
    title: req.body.title,
    jieshao: req.body.jieshao,
    photo: req.body.photo,
    type: req.body.type
  }

  await teamModel.addTeamMember(member).then(rows => {
    res.json({
      code: 200,
      message: '',
      detail: {}
    })
  }).catch(err => {
    res.json({
      code: 10001,
      message: '',
      detail: {}
    })
  })
}

adminControl.addBanner = async (req, res) => {
  const banner = {
    name: req.body.name,
    desc: req.body.desc,
    imgurl: req.body.imgurl,
    status: req.body.status
  }

  await bannerModel.addBanner(banner).then(row => {
    res.json({
      code: 200,
      message: '',
      detail: row
    })
  }).catch(err => {
    res.json({
      code: 100001,
      message: '',
      error: err
    })
  })
}

adminControl.getAllBanner = async (req, res) => {
  const status = {
    status: req.query.status
  }

  await bannerModel.getAllBanner().then(rows => {
    res.json({
      code: 200,
      message: '',
      detail: rows
    })
  }).catch(err => {
    res.json({
      code: 100001,
      message: '',
      detail: err
    })
  })
}

adminControl.updateBanner = async (req, res) => {
  const id = req.body.id
  const status = req.body.status

  await bannerModel.updateBannerStatus(id, status).then(rows => {
    res.json({
      code: 200,
      message: '',
      detail: rows
    })
  }).catch(err => {
    res.json({
      code: 10001,
      message: '',
      detail: err
    })
  })
}

module.exports = adminControl
